package com.sfh.cpc.com;

import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;

import com.sfh.cpc.com.server.INoteServer;
import com.sfh.cpc.com.server.IUserServer;
import com.sfh.lib.AppCacheManager;

import java.util.Base64;
import java.util.regex.Pattern;

/**
 * 功能描述: TODO
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class BaseAppContent extends Application {

    public static final String USER = "USER";

    private static final String URL_TEST = "http://172.168.10.177/";

    public static final String URL = "http://47.107.184.209/CPMASS/";


    public static String getUrl() {
        return URL;
    }

    public static String getImageUrl(String image) {
        return getUrl() + "sys/loadFile?image=" + image;
    }


    @Override
    public void onCreate() {
        super.onCreate();
       new  AppCacheManager.Builder(this).setCachePath("cpc").build();
    }

//    @Override
//    public void onLoseToken(String s) {
//        AppCacheManager.removeCache(USER);
//        // 打开应用首个打开界面
//        Intent i = this.getPackageManager().getLaunchIntentForPackage(
//                this.getPackageName());
//        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        this.startActivity(i);
//    }

    public void loadUserServer(IUserServer server) {
        AppCacheManager.putCache(IUserServer.class.getSimpleName(), server);
    }

    public void loadNoteServer(INoteServer server) {
        AppCacheManager.putCache(INoteServer.class.getSimpleName(), server);
    }

    public static boolean isBase64(String str) {
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, str);
    }

    public static String decode(String content) {
        if (!TextUtils.isEmpty(content) && isBase64(content)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                return new String(Base64.getDecoder().decode(content));
            } else {
                return new String(android.util.Base64.decode(content, android.util.Base64.DEFAULT));
            }
        }
        return content;
    }
}
